package com.micro.service.service;

import com.micro.service.dao.AccountRepository;
import com.micro.service.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
public class AccountUserService implements UserDetailsService {

    @Autowired
    private AccountRepository accountRepository;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Account account = accountRepository.findByUserName(s);
        if (account != null)
            return new User(account.getUserName(), account.getPassword(), AuthorityUtils.createAuthorityList(account.getRoles()));
        else
            throw new UsernameNotFoundException("用户[" + s + "]不存在");
    }
}
